﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
public class UIManager : Singleton_Base<UIManager>
{
    public override bool isDontDestroy => true;
    public PopupList_Scriptable popupList;
    public Queue<ObjectPack<Type, object[]>> popup_Queue;//正在等待的ui
    public UI_Popup_Base currentPopup;
    public Image popupCase;

    private void Start()
    {
        DontDestroyOnLoad(this.gameObject);

        EventTrigger.Entry onClickEntry = new EventTrigger.Entry();
        onClickEntry.eventID = EventTriggerType.PointerClick;
        onClickEntry.callback.AddListener((e) =>
        {
            if (currentPopup != null && currentPopup.isCloseByBG)
            {
                currentPopup.OnClose();
            }
        });
        popupCase.gameObject.AddComponent<EventTrigger>().triggers.Add(onClickEntry);

        EventManager.Instance.Regist(UI_Popup_Base.PopupActionType.PopupClose.ToString(), this.GetInstanceID(), (objects) => {
            UI_Popup_Base popup = (UI_Popup_Base)objects[0];
            if (popup_Queue.Count > 0)
            {
                //ObjectPack<string, object[]> objectPack= popup_Queue.Dequeue();
            }
        });
    }

    public T CreatePopup<T>(bool isWait, params object[] objects) where T : UI_Popup_Base<T>
    {
        if (!popupList.Contains(typeof(T)))
        {
            return null;
        }
        T popupPrefab = (T)popupList[typeof(T)];
        if (currentPopup != null )
        {
            currentPopup.OnClose();
        }
        T popup = ((T)Instantiate(popupPrefab, popupCase.transform)).Init(objects);
        return null;
    }
}
